package converters;

import beans.CargosNotaController;
import edu.upn.sigemfi.ope.be.CargosNota;
import javax.faces.convert.Converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

public class CargosNotaConverter implements Converter {

    public CargosNotaConverter() {
    }

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
        if (string == null) {
            return null;
        }
        try {
            Long id = Long.parseLong(string);
            return (new CargosNotaController()).buscar(id);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof CargosNota) {
            return ((CargosNota) object).getIdCargosNota().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo CargosNota");
        }
    }
}
